/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pb.diplom.first.gui.producerCategoryCommodity;

import com.pb.diplom.first.tables.CommodityJoin;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Slava
 */
public class AddOrUpdateCommodityPanel extends JPanel {

    //private JTextField idTextField;
    private JTextField modelTextField;
    private JTextField manufacturerField;
    private JTextField categoryTextField;
    private JTextField descriptionTextField;
    private JTextField priceTextField;
    private JTextField storageTextField;
    //private JTextField nameTextField;
    private JButton cancelButton;
    private JButton saveButton;

    private CommodityJoin commodity;

    public AddOrUpdateCommodityPanel() {
        super(new GridBagLayout());
        createUi();
        initListeners();
    }

    private void createUi() {
//        idTextField = new JTextField();
//        idTextField.setEnabled(false);
//        idTextField.setText("");
        modelTextField = new JTextField();
        manufacturerField = new JTextField();
        categoryTextField = new JTextField();
        descriptionTextField = new JTextField();
        priceTextField = new JTextField();
        priceTextField.setText("");
        storageTextField = new JTextField();
        //nameTextField = new JTextField();
        //idParentTextField = new JTextField();
        //idParentTextField.setEnabled(false);
        //idParentTextField.setText("");
        cancelButton = new JButton("Cancel");
        cancelButton.setPreferredSize(new Dimension(75, 20));
        saveButton = new JButton("Save");
        saveButton.setPreferredSize(new Dimension(75, 20));

        GridBagConstraints c = new GridBagConstraints();
        c.ipady = 5;
        c.weightx = 0.2;
        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.NONE;
        c.insets = new Insets(10, 20, 0, 0);
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 1;
        this.add(new JLabel("Model:"), c);

//        c.weightx = 0.8;
//        c.gridx = 1;
//        c.gridy = 0;
//        c.fill = GridBagConstraints.HORIZONTAL;
//        c.insets = new Insets(0, 0, 0, 20);
//        c.anchor = GridBagConstraints.WEST;
//        c.gridwidth = 3;
//        this.add(idTextField, c);
        c.weightx = 0.8;
        c.gridx = 1;
        c.gridy = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(0, 0, 0, 120);
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 3;
        this.add(modelTextField, c);

        c.weightx = 0.2;
        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(10, 20, 0, 0);
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 1;
        this.add(new JLabel("Manufacturer:"), c);

        c.weightx = 0.8;
        c.gridx = 1;
        c.gridy = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(0, 0, 0, 120);
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 3;
        this.add(manufacturerField, c);

        c.weightx = 0.2;
        c.gridx = 0;
        c.gridy = 2;
        c.insets = new Insets(10, 20, 0, 0);
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 1;
        this.add(new JLabel("Category:"), c);

        c.weightx = 0.8;
        c.gridx = 1;
        c.gridy = 2;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(0, 0, 0, 120);
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 3;
        this.add(categoryTextField, c);

        c.weightx = 0.2;
        c.gridx = 0;
        c.gridy = 3;
        c.insets = new Insets(10, 20, 0, 0);
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 1;
        this.add(new JLabel("Description:"), c);

        c.weightx = 0.8;
        c.gridx = 1;
        c.gridy = 3;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(0, 0, 0, 120);
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 3;
        this.add(descriptionTextField, c);

        c.weightx = 0.2;
        c.gridx = 0;
        c.gridy = 4;
        c.insets = new Insets(10, 20, 0, 0);
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 1;
        this.add(new JLabel("Price:"), c);
        
        c.weightx = 0.8;
        c.gridx = 1;
        c.gridy = 4;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(0, 0, 0, 120);
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 3;
        this.add(priceTextField, c);

        c.weightx = 0.2;
        c.gridx = 0;
        c.gridy = 5;
        c.insets = new Insets(10, 20, 0, 0);
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 1;
        this.add(new JLabel("Storage:"), c);
        
        c.weightx = 0.8;
        c.gridx = 1;
        c.gridy = 5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(0, 0, 0, 120);
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 3;
        this.add(storageTextField, c);

        c.weightx = 1;
        c.gridx = 2;
        c.gridy = 6;
        c.fill = GridBagConstraints.WEST;
        c.insets = new Insets(0, 20, 0, 20);
        c.gridwidth = 1;
        this.add(cancelButton, c);

        c.weightx = 1;
        c.gridx = 3;
        c.gridy = 6;
        c.fill = GridBagConstraints.EAST;
        c.insets = new Insets(0, 20, 0, 20);
        c.gridwidth = 1;
        this.add(saveButton, c);

        c.weightx = 1;
        c.gridx = 3;
        c.gridy = 3;
        c.fill = GridBagConstraints.EAST;
        c.insets = new Insets(0, 20, 0, 20);
        c.gridwidth = 1;
    }

    private void initListeners() {
        cancelButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                clear();
            }
        });

    }

    void addSaveBtnListener(ActionListener listener) {
        saveButton.addActionListener(listener);
    }

    //TODO: implement error logic and add error Label.
    void setErrorMessage(String text) {

    }

    void setProducer(CommodityJoin commodity) {
        this.commodity = commodity;
        modelTextField.setText(commodity.getModel());
        manufacturerField.setText(commodity.getNameManufacturer());
        categoryTextField.setText(commodity.getNameCategory());
        descriptionTextField.setText(commodity.getDescription());
        priceTextField.setText(commodity.getPrice().toString());
        storageTextField.setText(commodity.getStorage());
    }

    void clear() {
        commodity = null;
        modelTextField.setText("");
        manufacturerField.setText("");
        categoryTextField.setText("");
        descriptionTextField.setText("");
        priceTextField.setText("");
        storageTextField.setText("");

    }
//Long id, Long producerId, Long categoryId, String model, BigDecimal price, String description, String image, String storage

    CommodityJoin getCommodity() {

        //return new CommodityJoin();nameTextField.getText(), nameTextField.getText(),
        return new CommodityJoin(modelTextField.getText(), manufacturerField.getText(), categoryTextField.getText() , descriptionTextField.getText(), commodity != null ? commodity.getPrice() : null,  storageTextField.getText());
    }
//return new Category(category != null ? category.getId() : null, category.getParentId() : null, category.getName() : null,nameTextField.getText());
}
